package client.GUI.login;

import javax.swing.*;
import client.GUI.GUIClient;
import client.GUI.GUIConstants;
import client.backend.Connection;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * Sets up the panel that allows the user to login
 * 
 */
public class LoginPanel extends JPanel implements ActionListener
{
	private static LoginPanel instance = null;
	
    JButton login, quit, forgotPassword;
    JLabel uNameLabel, pWordLabel, messageLabel;
    JPasswordField password;
    JTextField username;
    
    final int LoginX = 576;
    final int LoginY = 768 - 192;
    final int LoginW = 128;
    final int LoginH = 30;
    
    final int QuitX = LoginX;
    final int QuitY = 768 - 144;
    final int QuitW = LoginW;
    final int QuitH = LoginH;
    
    final int UsernameX = 256 + 96;
    final int UsernameY = LoginY;
    final int UsernameW = 208;
    final int UsernameH = LoginH;
    
    final int PasswordX = UsernameX;
    final int PasswordY = QuitY;
    final int PasswordW = UsernameW;
    final int PasswordH = LoginH;
    
    final int uLabelX = 256;
    final int uLabelY = LoginY;
    final int uLabelW = 96;
    final int uLabelH = LoginH;;
    
    final int pLabelX = uLabelX;
    final int pLabelY = QuitY;
    final int pLabelW = uLabelW;
    final int pLabelH = LoginH;
    
    final int messageX = 50;
    final int messageY = 768-100;
    final int messageW = 796;
    final int messageH = 30;
    
    final int fPX = 846;
    final int fPY = messageY;
    final int fPW = 128;
    final int fPH = 30;

    /**
     * Default constructor, sets the size, layout and
     * creates the components
     */
    private LoginPanel() {
    	super();
        this.setSize(1024,768);
        this.setLayout(null);
        
        this.setBackground(GUIConstants.Background_Color);
        
        login = new JButton("LOGIN");
        quit = new JButton("QUIT");
        uNameLabel = new JLabel("Username");
        pWordLabel = new JLabel("Password");
        password = new JPasswordField();
        username = new JTextField();
        forgotPassword = new JButton("New Password");
        messageLabel = new JLabel();
        
        login.setBounds(LoginX, LoginY, LoginW, LoginH);
        quit.setBounds(QuitX, QuitY, QuitW, QuitH);
        uNameLabel.setBounds(uLabelX, uLabelY, uLabelW, uLabelH);
        pWordLabel.setBounds(pLabelX, pLabelY, pLabelW, pLabelH);
        username.setBounds(UsernameX, UsernameY, UsernameW, UsernameH);
        password.setBounds(PasswordX, PasswordY, PasswordW, PasswordH);
        forgotPassword.setBounds(fPX, fPY, fPW, fPH);
        messageLabel.setBounds(messageX, messageY, messageW, messageH);
        
        uNameLabel.setForeground(GUIConstants.Text_Color);
        uNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        pWordLabel.setForeground(GUIConstants.Text_Color);
        pWordLabel.setHorizontalAlignment(SwingConstants.CENTER);
        messageLabel.setForeground(GUIConstants.Text_Color);
        messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        
        login.addActionListener(this);
        quit.addActionListener(this);
        forgotPassword.addActionListener(this);
        forgotPassword.setVisible(false);
        
        this.add(login);
        this.add(quit);
        this.add(uNameLabel);
        this.add(pWordLabel);
        this.add(username);
        this.add(password);
        this.add(messageLabel);
        this.add(forgotPassword);
    }
    
    /**
     * Checks for an existing instance of LoginPanel
     * @return	Instance of LoginPanel
     */
    public static LoginPanel getInstance() {
    	if (instance == null)
    		instance = new LoginPanel();
    	
    	return instance;
    }
    
    /**
     * Sets the username field
     * 
     * @param uname		The String holding the user name
     */
    public void setFields(String uname) {
        username.setText(uname);
    }
    
    /**
     * Sets the username and password fields
     * 
     * @param uname		The String holding the user name
     * @param pword		The String holding the password
     */
    public void setFields(String uname, String pword) {
        username.setText(uname);
        password.setText(pword);
    }
    
    /**
     * Checks for actions and performs the appropriate tasks.
     */
    public void actionPerformed(ActionEvent e) {
    	if (e.getSource() == quit)
    		System.exit(0);
    	else if (e.getSource() == login) {
    		String response = Connection.getInstance().login(username.getText(), String.valueOf(password.getPassword()));
    		if (response.startsWith("OK")) {
    			GUIClient.getInstance().loggedIn();
    		}
    		else
    			incorrectPassword(response);
    	}
    	else if (e.getSource() == forgotPassword) {
    		GUIClient.getInstance().forgotPassword(username.getText());
    	}
    }
    
    /**
     * Displays a message in the case of an incorrect password
     * and displays the forgot password button
     */
    public void incorrectPassword(String response) {
    	response = response.substring(7);
    	messageLabel.setText(response);
    	forgotPassword.setVisible(true);
    }
}

